// pages/home/home.js

const app = getApp();

let pageOptions = {
  startX: 0,
  startY: 0,
  pX: 0,
  pY: 0,
  x: 0,
  y: 0
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    x: -120,
    y: 0,
    userinfo:null,
    canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName') // 如需尝试获取用户信息可改为false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.getSystemInfo({
      success: (result) => {
        this.setData({
          windowWidth: result.windowWidth,
          windowHeight: result.windowHeight
        });
      },
    });
    if(app.globalData.userinfo){
      this.setData({
        userinfo:app.globalData.userinfo
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  handlestart(event) {
    pageOptions.startX = event.touches[0].clientX - this.data.x;
    pageOptions.startY = event.touches[0].clientY - this.data.y;
  },
  handlemove(event) {
    // let x = pageOptions.x;
    let px = event.touches[0].clientX - pageOptions.startX;
    let py = event.touches[0].clientY - pageOptions.startY;
    if (px < 0 && px > this.data.windowWidth - 600) {
      this.setData({
        x: Math.floor(px)
      });
    }
    if (py < 0 && py > this.data.windowHeight - 800) {
      this.setData({
        y: Math.floor(py)
      });
    }

    // this.setData({
    //   x:px
    // });
    // console.log(py)
  },
  imgLoadFunc(event) {
    console.log(event, 'event');
  },
  handlepath(event) {
    // console.log(event,'event');
    this.getUserInfo().then(res=>{
      let path = event.target.dataset.path;
      wx.navigateTo({
        url: path,
      });
    })
  },
  getUserInfo() {
    return new Promise((resolve,reject)=>{
      if(this.data.userinfo){
        return resolve();
      }
      wx.getUserProfile({
        desc: '用于完善会员资料',
        success: (res) => {
          const userinfo = res.userInfo;
          userinfo.sex = userinfo.gender === 1 ? "男" : "女";
 
          // this.globalData.userinfo = userinfo;
          app.globalData.userinfo = userinfo;
          this.setData({
            userinfo
          });
          resolve();
        }
      })
    })
  }
})